﻿
using IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    /// <summary>
    /// 泛型方法
    /// where T : class只能是引用类型
    /// </summary>
    /// <typeparam name="T">泛型接口</typeparam>
    public class BaserDal<T> : IBaserDal<T> where T : class
    {
        /// <summary>
        /// 实现构造函数
        /// </summary>
        /// <param name="context"></param>
         public BaserDal(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回值受影响行数</returns
        public async  Task<int> AddAnync(T t)
        {
            await Context.Set<T>().AddAsync(t);
             return await Context.SaveChangesAsync();


        }
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回值受影响行数</returns>
        public async Task<int> AddRangeAnync(List<T> list)
        {
            await Context.Set<T>().AddRangeAsync(list);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回值受影响行数</returns>
        public async Task<int> DeleteAnync(T t)
        {
            Context.Set<T>().Remove(t);
            return await Context.SaveChangesAsync();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回值受影响行数</returns>
        public async Task<int> DeleteRangeAnync(List<T> list)
        {
            Context.Set<T>().RemoveRange(list);
            return await Context.SaveChangesAsync();
        }

        /// <summary>
        /// 延迟（查询）
        /// </summary>
        /// <returns>返回值受影响行数</returns>
        public IQueryable<T> GetValues()
        {
            return Context.Set<T>();

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回值受影响行数</returns>
        public async Task<int> UpdateAnync(T t)
        {
            Context.Set<T>().Update(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回值受影响行数</returns>
        public async Task<int> UpdateRangeAnync(List<T> list)
        {
            Context.Set<T>().UpdateRange(list);
            return await Context.SaveChangesAsync();
        }
    }
}
